package cn.k.controller;

import cn.k.domain.Permission;
import cn.k.qo.JSONResult;
import cn.k.qo.QueryObject;
import cn.k.service.IPermissionService;
import cn.k.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.sql.SQLOutput;
import java.util.*;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private ApplicationContext ctx;

    //@Autowired
    //Spring MVC启动容器的时候帮我们创建的对象,这个对象负责根据请求找对应处理方法
    //private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Autowired
    private IPermissionService permissionService;

    //处理分页查询页面请求
    @RequestMapping("/list")
    public String list(QueryObject qo, Model model) {
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";

        //获取容器上的Controller注解
        //ctx.getBeansWithAnnotation(Controller.class);
    }


    //处理删除请求
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            permissionService.delete(id);
        }
        return "redirect:/permission/list";
    }

    //响应JSON格式数据 {"success":true,"msg":"加载成功"}
    @RequestMapping("/load")
    @ResponseBody
    public JSONResult load() {
        //HandlerMethod Spring MVC对我们写处理方法进步的封装
        /*Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
        Collection<HandlerMethod> handlerMethods = map.values();
        for (HandlerMethod handlerMethod : handlerMethods) {
            System.out.println(handlerMethod);
        }*/

        try {

            //查询数据库中所有的权限
            List<Permission> permissionAll = permissionService.selectAll();
            //定义一个存权限Set集合
            Set<Permission> permissions = new LinkedHashSet<>();


            //如何知道所有控制器的处理方法,转成数据库权限表中的一条条数据
            Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
            //获取所有控制器对象
            Collection<Object> controllers = map.values();
            for (Object controller : controllers) {
                //获取控制器对象本类中的方法
                Method[] methods = controller.getClass().getDeclaredMethods();
                for (Method method : methods) {
                    //获取方法的注解
                   /* RequirePermission annotation = method.getAnnotation(RequirePermission.class);
                    if (annotation != null) {

                        Permission permission = new Permission();
                        permission.setName(annotation.name());
                        permission.setExpression(annotation.expression());

                        //存到set之前判断一下,看这个权限是否在数据库中已存在,若不存在才存到set,之后插入
                        if (!permissionAll.contains(permission)) {
                            permissions.add(permission);
                        }
                    }*/
                }
            }

            if (permissions.size() > 0) {
                //循环把所有要保存的权限数据封装好,存到set集合中
                System.out.println(permissions.size());
                permissionService.batchSave(permissions);
            }
            return new JSONResult(true, "加载成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "加载失败");
        }

    }


    //处理添加或者更新的请求
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission) {
        //判断有没有id
        if (permission.getId() == null) {
            permissionService.insert(permission);
        } else {
            permissionService.update(permission);
        }
        return "redirect:/permission/list";
    }


}
